﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoritmTeorija
{
    class FileHelper
    {
        private BinaryReader _reader;
        private BinaryWriter _writer;
        private string _fileName;
        private long _length;
        public int opCount = 0;

        public FileHelper(string filename, FileMode mode)
        {
            this._fileName = filename;
            if(mode == FileMode.Create)
                FileClear();
            if (!File.Exists(_fileName))
                FileClear();
            SetLength();
        }
       
        private void FileClear()
        {
            _writer = new BinaryWriter(File.Open(_fileName, FileMode.Create));
            _writer.Close();
        }

        private void SetLength()
        {
            _reader = new BinaryReader(File.Open(_fileName, FileMode.Open));
            _length = _reader.BaseStream.Length;
            _reader.Close();
        }

        public void WriteBinary(int sk)
        {
            _writer = new BinaryWriter(File.Open(_fileName, FileMode.Open));
            _writer.BaseStream.Position = _length;
            _writer.Write(sk);
            _length += sizeof (int);
            _writer.Close();
            opCount++;
        }

        public void WriteBinary(int pos, int sk)
        {
            if (pos * sizeof(int) > _length)
            {
                throw new IndexOutOfRangeException("Position you specified is out of range!");
            }

            if (pos * sizeof(int) == _length)
                _length += sizeof (int);

            _writer = new BinaryWriter(File.Open(_fileName, FileMode.Open));
            _writer.BaseStream.Position = pos * sizeof(int);
            _writer.Write(sk);
            _writer.Close();
            opCount++;
        }

        public int ReadBinary(int pos)
        {
            if (pos * sizeof(int) >= _length)
                throw new IndexOutOfRangeException("Position you specified is out of range!");

            _reader = new BinaryReader(File.Open(_fileName, FileMode.Open));
            _reader.BaseStream.Position = pos * sizeof(int);

            int result = _reader.ReadInt32();

            _reader.Close();
            opCount++;

            return result;
        }

        public int Length
        {
            get
            {
                return (int)this._length/sizeof (int);
            }
            
        }

        public string FileName
        {
            get { return _fileName; }
        }

        public void PrintFromFile()
        {
            _reader = new BinaryReader(File.Open(_fileName, FileMode.Open));
            var pos = 0;
            var lenght = _reader.BaseStream.Length;
            while (pos < lenght)
            {
                Console.WriteLine(_reader.ReadInt32());
                pos += sizeof (int);
            }
            _reader.Close();
        }

        public string GetAllFile()
        {
            string data = "";
            _reader = new BinaryReader(File.Open(_fileName, FileMode.Open));
            var pos = 0;
            var lenght = _reader.BaseStream.Length;
            while (pos < lenght)
            {
                data += _reader.ReadInt32() + "\r\n";
                pos += sizeof(int);
            }
            _reader.Close();

            return data.TrimEnd("\r\n".ToCharArray());
        }

        public string CopyFile(string fileName)
        {
            string newFileName = "copyOf" + fileName;

            _reader = new BinaryReader(File.Open(_fileName, FileMode.Open));
            _writer = new BinaryWriter(File.Open(newFileName, FileMode.Create));
            var pos = 0;
            var lenght = _reader.BaseStream.Length;
            while (pos < lenght)
            {
                _writer.Write(_reader.ReadInt32());
                pos += sizeof(int);
            }
            _reader.Close();
            _writer.Close();

            _fileName = newFileName;

            return newFileName;
        }
    }
}
